package ph.com.gs3.formalistics.presenter.fragments.views;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.global.utilities.Validator;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginViewFragment extends Fragment {

	public static final String TAG = LoginViewFragment.class.getSimpleName();

	private TextView tvMessage;

	private EditText etServer;
	private EditText etEmail;
	private EditText etPassword;

	private Button bSignIn;

	private LoginActionsListener listener;

	private String queuedErrorMessage = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_login, container, false);

		initializeComponents(rootView);
		initializeActions(rootView);
		runThroughQueuedTasks();

		return rootView;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			listener = (LoginActionsListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
			        + " must implement LoginActionsListener");
		}
	}

	private void initializeComponents(View rootView) {

		// fields
		tvMessage = (TextView) rootView.findViewById(R.id.login_tvMessage);

		etServer = (EditText) rootView.findViewById(R.id.login_etServer);
		etEmail = (EditText) rootView.findViewById(R.id.login_etEmail);
		etPassword = (EditText) rootView.findViewById(R.id.login_etPassword);

		// button(s)
		bSignIn = (Button) rootView.findViewById(R.id.login_bSignIn);

		// Mock Data
		etServer.setText("192.168.0.90");

		// etEmail.setText("ervinne.sodusta@gs3.com.ph");
		// etEmail.setText("ervinne.dummy@gs3.com.ph");
		// etEmail.setText("admin@smartmatic.com.ph");
		etEmail.setText("charnie.capulong@gs3.com.ph");
		// etEmail.setText("roni.pinili@gs3.com.ph");
		// etEmail.setText("jagliam@yahoo.com");

		// etEmail.setText("jen@qac.com");

		etPassword.setText("password");

	}

	private void runThroughQueuedTasks() {

		if (queuedErrorMessage != null) {
			setErrorMessage(queuedErrorMessage);
			queuedErrorMessage = null;
		}

	}

	private void initializeActions(View rootView) {

		final Context context = rootView.getContext();

		bSignIn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				if (validateFields(context)) {
					listener.onSignIn(etServer.getText().toString(), etEmail.getText().toString(),
					        etPassword.getText().toString());

					InputMethodManager imm = (InputMethodManager) context
					        .getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(etPassword.getWindowToken(), 0);
				}
			}
		});

	}

	private boolean validateFields(Context context) {

		int serverValidity = Validator.validateNonEmpty(etServer.getText().toString());
		int emailValidity = Validator.validateEmail(etEmail.getText().toString());
		int passwordValidity = Validator.validatePassword(etPassword.getText().toString());

		boolean isValid = true;

		if (serverValidity == Validator.EMPTY) {
			etServer.setError(context.getString(R.string.validation_empty));
			isValid = false;
		}

		if (emailValidity == Validator.EMPTY) {
			etEmail.setError(context.getString(R.string.validation_empty));
			isValid = false;
		} else if (emailValidity == Validator.INVALID) {
			etEmail.setError(context.getString(R.string.validation_invalid_email));
			isValid = false;
		}

		if (passwordValidity == Validator.EMPTY) {
			etPassword.setError(context.getString(R.string.validation_empty));
			isValid = false;
		} else if (passwordValidity == Validator.TOO_SHORT) {
			etPassword.setError(context.getString(R.string.validation_too_short));
			isValid = false;
		}

		return isValid;

	}

	public void setErrorMessage(String message) {

		if (isAdded()) {
			tvMessage.setText(message);
			tvMessage.setTextColor(Color.parseColor("#EB1010"));
			tvMessage.setVisibility(View.VISIBLE);
		} else {
			queuedErrorMessage = message;
		}

	}

	public static interface LoginActionsListener {
		public void onSignIn(String server, String email, String password);
	}

}
